package com.insigma.business.qrcode.common.util;

import com.alibaba.fastjson.JSON;
import com.insigma.business.common.dto.CurrentUser;
import com.insigma.business.common.service.CurrentUserService;
import com.insigma.business.common.utils.CurrentUserUtil;
import com.insigma.business.qrcode.common.dto.RpcContextAttachmentConstant;
import com.insigma.business.qrcode.common.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class RpcContextUtil {
    private static final ThreadLocal<Map<String, String>> rpcContextCache = new ThreadLocal();

    @Autowired
    private CurrentUserService currentUserService;

    private static CurrentUserService currentUserServiceUtil;

    @PostConstruct
    public void init() {
        currentUserServiceUtil = currentUserService;
    }

    private RpcContextUtil() {
    }

    public static String getTraceId() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_TRACE_ID);
    }

    public static void setTraceId(String traceId) {
        setupMapIfAbsent();
        ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_TRACE_ID, traceId);
    }

    public static String getNowSpanId() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SPAN_ID);
    }

    public static void setNowSpanId(String traceId) {
        setupMapIfAbsent();
        ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SPAN_ID, traceId);
    }

    public static String getToken() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_TOKEN);
    }

    public static String setToken(String token) {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_TOKEN, token);
    }

    public static String getSessionSoldierStr() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_SOLDIER);
    }

    public static SessionSoldier getSessionSoldier() {
//        setupMapIfAbsent();
//        String sessionSoldierStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_SOLDIER);
//        return StringUtils.isNotBlank(sessionSoldierStr) ? (SessionSoldier) JSON.parseObject(sessionSoldierStr, SessionSoldier.class) : null;

        SessionSoldier sessionSoldier = new SessionSoldier();
        CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
        sessionSoldier.setUserId(currentUser.getSoldierId());
        sessionSoldier.setMobile(currentUser.getMobile());
        sessionSoldier.setUserType(currentUser.getUserType());
        return sessionSoldier;
    }

    public static String setSessionSoldierStr(String sessionSoldierStr) {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_SOLDIER, sessionSoldierStr);
    }

    public static String getSessionGovEmployeeStr() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_GOV_EMPLOYEE);
    }

    public static SessionGovEmployee getSessionGovEmployee() {
        setupMapIfAbsent();
        String sessionGovEmployeeStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_GOV_EMPLOYEE);
        return StringUtils.isNotBlank(sessionGovEmployeeStr) ? (SessionGovEmployee) JSON.parseObject(sessionGovEmployeeStr, SessionGovEmployee.class) : null;
    }

    public static String setSessionGovEmployeeStr(String sessionGovEmployeeStr) {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_GOV_EMPLOYEE, sessionGovEmployeeStr);
    }

    public static String getSessionCommonUserStr() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMMON_USER);
    }

    public static SessionCommonUser getSessionCommonUser() {
        setupMapIfAbsent();
        String sessionCommonUserStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMMON_USER);
        return StringUtils.isNotBlank(sessionCommonUserStr) ? (SessionCommonUser) JSON.parseObject(sessionCommonUserStr, SessionCommonUser.class) : null;
    }

    public static String setSessionCommonUserStr(String sessionCommonUserStr) {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMMON_USER, sessionCommonUserStr);
    }

    public static String getSessionCompanyUserStr() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMPANY_USER);
    }

    public static SessionCompanyUser getSessionCompanyUser() {
        setupMapIfAbsent();
        String sessionCompanyUserStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMPANY_USER);
        return StringUtils.isNotBlank(sessionCompanyUserStr) ? (SessionCompanyUser) JSON.parseObject(sessionCompanyUserStr, SessionCompanyUser.class) : null;
    }

    public static String setSessionCompanyUserStr(String sessionCompanyUserStr) {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMPANY_USER, sessionCompanyUserStr);
    }

    public static SessionMerchantUser getSessionMerchantUser() {
        setupMapIfAbsent();
        String sessionMerchantUserStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_MERCHANT_USER);
        return StringUtils.isNotBlank(sessionMerchantUserStr) ? (SessionMerchantUser) JSON.parseObject(sessionMerchantUserStr, SessionMerchantUser.class) : null;
    }

    
    public static String setSessionMerchantUserStr(String sessionMerchantUserStr) {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_MERCHANT_USER, sessionMerchantUserStr);
    }

    public static String getSessionMerchantUserStr() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_MERCHANT_USER);
    }


    public static BaseSessionUser getBaseSessionUser() {
        setupMapIfAbsent();

        String sessionMerchantUserStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_MERCHANT_USER);
        if (StringUtils.isNotBlank(sessionMerchantUserStr)) {
            return (BaseSessionUser) JSON.parseObject(sessionMerchantUserStr, SessionMerchantUser.class);
        }

        String sessionCompanyUserStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_COMPANY_USER);
        if (StringUtils.isNotBlank(sessionCompanyUserStr)) {
            return (BaseSessionUser) JSON.parseObject(sessionCompanyUserStr, SessionCompanyUser.class);
        }

        String sessionGovEmployeeStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_GOV_EMPLOYEE);
        if (StringUtils.isNotBlank(sessionGovEmployeeStr)) {
            return (BaseSessionUser) JSON.parseObject(sessionGovEmployeeStr, SessionGovEmployee.class);
        }

        String sessionSoldierStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_SOLDIER);
        return StringUtils.isNotBlank(sessionSoldierStr) ? (BaseSessionUser) JSON.parseObject(sessionSoldierStr, SessionSoldier.class) : null;
    }

    public static String getSessionBasisStr() {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_BASIS);
    }

    public static String setSessionBasisStr(String sessionBasisStr) {
        setupMapIfAbsent();
        return (String) ((Map) rpcContextCache.get()).put(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_BASIS, sessionBasisStr);
    }

    public static SessionBasis getSessionBasis() {
        setupMapIfAbsent();
        String sessionBasisStr = (String) ((Map) rpcContextCache.get()).get(RpcContextAttachmentConstant.RPC_CONTEXT_KEY_SESSION_BASIS);
        return StringUtils.isNotBlank(sessionBasisStr) ? (SessionBasis) JSON.parseObject(sessionBasisStr, SessionBasis.class) : null;
    }

    private static void setupMapIfAbsent() {
        if (rpcContextCache.get() == null) {
            rpcContextCache.set(new HashMap());
        }

    }

    public static void clear() {
        rpcContextCache.remove();
    }
}
